import jieba

# 读取文本
txt = open("test.txt", "r", encoding="utf-8").read()

# 分词
cuts = jieba.lcut(txt)

# 去掉标点符号，统计每个词汇出现的次数
res = {}
for i in cuts:
    if len(i) < 2:
        continue
    res[i] = res.get(i, 0) + 1

# 获取出现次数前五的词汇
ls = []
for i in res:
    ls.append((i, res[i]))
ls.sort(key=lambda s: s[1],reverse=True)
ls = ls[:5]

# 打印结果
print("出现次数最多的前五个词：", ls)
print("所有词的数量：", len(res))

# 写入文件
fw = open("output.txt", "w", encoding="utf-8")
for i in ls:
    fw.write("词汇：{}，出现次数：{}\n".format(i[0], i[1]))
fw.close()
